<template>
	<view class="finish">
		<!-- 完成签署 提醒弹出框 签署人 -->
		<uni-popup ref="sealopenDialog" type="dialog" background-color="#fff" style="z-index:999" :is-mask-click="false">
			<uni-popup-dialog :type="msgType" cancelText="了解碳能量" confirmText="领取碳能量" title="通知" @confirm="getCarbonEnergy" @close="dialogClose" :before-close="true">
		  <view class="popup-content" v-slot="content">
			<i class="iconfont themeColor">&#xe69e;</i>
			<h2 style="font-weight: 550;" class="themeColor">恭喜，您本次已成功签署！</h2>
			<view class="dialog-footer">
				<button size="mini" @click="back">返回工作台</button>
				<button @click="close" size="mini">查看签署文件</button>
			</view>
			<view class="grayColor">感谢使用绿色签约，为美丽的地球节约一次森林资源！</view>
			<p class="flex_p">本次合同签署，励氪签奖励您【{{addIntegral}}g】碳能量！</p>
			<!-- <view class="carbonEnergy themeColor"><a href="https://www.lksign.cn/carbonEnergy.html" target="_blank" class="plan">了解碳能量计划，</a><span @click="getCarbonEnergy">立即领取</span></view> -->
		  </view>
		  </uni-popup-dialog>
		</uni-popup>
		
		<!-- 完成签署 提醒弹出框 经办人 -->
		<uni-popup ref="openDialog" type="dialog" background-color="#fff" style="z-index:999" :is-mask-click="false">
			<uni-popup-dialog :type="msgType" cancelText="了解碳能量" confirmText="领取碳能量" title="通知" @confirm="getCarbonEnergy" @close="dialogClose" :before-close="true">
		  <view class="popup-content" v-slot="content">
			<i class="iconfont themeColor">&#xe69e;</i>
			<h2 style="font-weight: 550;" class="themeColor">恭喜，您经办的合同已签署！</h2>
			<view class="dialog-footer">
				<button size="mini" @click="back">返回工作台</button>
				<button @click="close" size="mini">查看签署文件</button>
			</view>
			<view class="grayColor">感谢使用绿色签约，为美丽的地球节约一次森林资源！</view>
			<p class="flex_p">本次合同签署，励氪签奖励您【{{addIntegral}}g】碳能量！</p>
			<!-- <view class="carbonEnergy themeColor"><a href="https://www.lksign.cn/carbonEnergy.html" target="_blank" class="plan">了解碳能量计划，</a>><span @click="getCarbonEnergy">立即领取</span></view> -->
		  </view>
		  </uni-popup-dialog>
		</uni-popup>
					
	</view>
</template>

<script>
	import {getContractSignMine} from "@/api/ec/work/contract/contractSign";
	import {getSignIntegral} from "@/api/ec/work/contract/contract";
	export default {
		data() {
			return {
				msgType:'info',
				addIntegral:0,// 能领取的碳能量
				flowId:null,
				url:'https://www.lksign.cn/carbonEnergy.html',
			}
		},
		methods: {
			// 了解查看碳能量
			dialogClose(){
				window.open("about:blank").location.href = this.url;
			},
			back(){
				uni.switchTab({
					url:'/pages/index'
				})
			},
			getCarbonEnergy(){
			   getSignIntegral(this.flowId).then(response => {
			       this.$modal.msgSuccess("领取成功")
			   });
			},   
			openDialog(flows,flowId){
				this.flowId = flowId;
				// 判断不为空 则是签署页面过来的 然后查询签署状态 即可查询当前签署流程的签署状态
				if (!!flowId) {
          // 判断缓存中是否有已签署完成的弹窗 否则就不进行弹窗
          if (!this.$cache.local.get("signFinish" + flowId)) {
            getContractSignMine(flowId).then((response) => {
              let data = response.data;
              // 判断当前流程的签署状态
              if (data.status == 2 || data.status == 10) {
                this.sumIntegral(flows, data)
                // 签署人显示
                if (data.sealerId == this.$store.getters.staffInfo.id) {
                  this.$refs.sealopenDialog.open()
                  this.$cache.local.set("signFinish" + flowId, flowId);
                } else {
                  // 经办人显示
                  this.$refs.openDialog.open()
                  this.$cache.local.set("signFinish" + flowId, flowId);
                }
              }
            });
          }
				}  
			},
			// 计算当前签署方奖励碳能量数量
			sumIntegral(flows,contractSignFlowDb){
			  // 计算签署方 每方分多少个 若有经办人和盖章人 余数给经办人
			  let addIntegral = parseInt(20 / (flows.contractSignFlowList.length+1));// 默认平分
			  if (contractSignFlowDb.sealerId != contractSignFlowDb.systemStaffId) {
			    // 经办人和盖章人不是同一人 若能除尽则平分
			    if (addIntegral % 2 == 0) {
			      addIntegral = addIntegral / 2;
			    } else {
			      // 若不能除尽 把余数给经办人
			      if (contractSignFlowDb.systemStaffId == this.$store.getters.staffId) {
			        addIntegral = parseInt(addIntegral / 2) + (addIntegral % 2);
			      } else {
			        // 盖章人
			        addIntegral = parseInt(addIntegral / 2);
			      }
			    }
			  }
			  this.addIntegral = addIntegral;
			},    
			close(){
				this.$refs.sealopenDialog.close()
				this.$refs.openDialog.close()
			},
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .uni-dialog-title{
		display: none;
	}
	.carbonEnergy{
		font-size: 26rpx;
	}
	.plan{
		color: #999;
		text-decoration: none;
	}
	.flex_p{
		margin: 14rpx;
	}
	.grayColor{
		font-size: 26rpx;
		color: #999;
	}
	.iconfont{
		display: block;
		font-size: 90rpx;
	    margin: 40rpx auto 20rpx;
	}
	.dialog-footer{
		margin: 40rpx auto;
		display: flex;
		justify-content: space-between;
		width: 78%;
		button:first-child{
			background: #fff;
			border: 2rpx solid #ccc;
			
		}
		button:last-child{
			background: $uni-color-primary;
			color: #fff;
			border: 2rpx solid $uni-color-primary;
		}
	}
	.popup-content{
		text-align: center;
		margin: 0 auto;
	}
	.finish ::v-deep .uni-dialog-content{
		padding: 0;
	}
	::v-deep .uni-popup-dialog{
		width: 90%;
	}
	::v-deep .uni-popup .uni-popup__wrapper{
		width: 90%;
	}
	
</style>
